package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Application;
import com.example.demo.entity.Exam;
import com.example.demo.entity.Licence;
import com.example.demo.entity.Progress;
import com.example.demo.mapper.ApplicationMapper;
import com.example.demo.mapper.ExamMapper;
import com.example.demo.mapper.ProgressMapper;
import com.example.demo.service.ProgressService;
import jakarta.transaction.Transactional;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class ProgressImpl implements ProgressService {
    private final ExamMapper examMapper;
    private final ApplicationMapper applicationMapper;
    private final ProgressMapper progressMapper;
    private final ApplicationImpl applicationImpl;
    @Override
    public Progress updateByScore(String score,Long examId) {
        Long studentId = examMapper.findById(examId).getStudentId();
        Long applicationId = applicationImpl.findApplication(studentId).getApplicationId();
        Progress p = progressMapper.findProgressByApplicationId(applicationId);
        int s = Integer.parseInt(score);
        if(s<60){
            p.setProgressState("不合格");
        }
        else{
            p.setProgressState("合格");
        }
        try {
        progressMapper.updateProgress(p.getProgressId(),p.getApplicationId(),
                p.getSubjectId(), p.getProgressState());
            return p;
        }
        catch (Exception e){
            throw new RuntimeException("Failed to update Progress");
        }
    }
    @Override
    public List<Progress> findAllProgress() {
        return progressMapper.findAllProgress();
    }

    @Override
    public Progress findByStudentID(Long studentId) {
        Long appID = applicationImpl.findApplication(studentId).getApplicationId();
        return progressMapper.findProgressByApplicationId(appID);
    }

    @Override
    public Progress addProgress(Progress progress) {
        try {
            progressMapper.insertProgress(progress.getProgressId(),progress.getApplicationId(),
                    progress.getSubjectId(),progress.getProgressState());
            return progress;
        }catch (Exception e){
            throw new RuntimeException("Failed to add Progress");
        }
    }

    @Override
    public Long deleteProgress(Long progressId) {
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("licence_id",licenceId);
        int result = progressMapper.deleteById(progressId);
        if(result>0){
            return progressId;
        }
        else {
            throw new RuntimeException("Failed to delete Progress");
        }
    }

    @Override
    public Progress updateProgress(Progress progress) {
        try {
            progressMapper.updateProgress(progress.getProgressId(),progress.getApplicationId(),
                    progress.getSubjectId(),progress.getProgressState());
            return progress;
        }catch (Exception e){
            throw new RuntimeException("Failed to update Exam");
        }
    }
    @Override
    public Map<Map<String,Integer>, Date> findApplicationAndExam(Long studentId){
        Map<Map<String,Integer>,Date> map = new HashMap<>();
        Progress p = findByStudentID(studentId);
        Application application = p.getApplication();
        Licence licence = application.getLicence();

        Map<String,Integer> m1 = new HashMap<>();
        m1.put(licence.getLicenceType(),0);
        map.put(m1,application.getApplicationTime());
        List<Exam> list =examMapper.findByStudentId(studentId);
        Map<String,Integer> m2 = new HashMap<>();
        for (int i = 0; i<list.size();i++)
        {
            if(list.get(i).getLicenceId()==application.getLicenceId()){
                String subject = list.get(i).getSubject().getSubjectType();
                if(!m2.containsKey(subject)){
                    m2.put(subject,1);
                }
                else {
                    m2.replace(subject,m2.get(subject)+1);
                }
                if(list.get(i).getExamScore().compareTo("60")>=0){
                    Map<String,Integer> m3 = new HashMap<>();
                    m3.put(subject,m2.get(subject));
                    map.put(m3,list.get(i).getExamTime());
                }
            }
        }
        return map;
    }
}
